#include "APPAlgorithmDesigner.h"
#include "ui_APPAlgorithmDesigner.h"

#include <QMessageBox>

using namespace RafMat::SimInterface;

APPAlgorithmDesigner::APPAlgorithmDesigner(QWidget *parent) :
    AdaptiveAlgorithmDesignerBase(parent),
    ui(new Ui::APPAlgorithmDesigner)
{
    ui->setupUi(this);
}

APPAlgorithmDesigner::~APPAlgorithmDesigner()
{
    delete ui;
}

void APPAlgorithmDesigner::attachAlgorithm(RafMat::SimInterface::IConfigurableSPtr ctrl)
{
    m_targetAlgorithm = ctrl;

    try
    {
        ui->edAmPoly->setText(QString::fromStdString(m_targetAlgorithm->getParameter("adpt::ampoly")));
        ui->edBPoly->setText(QString::fromStdString(m_targetAlgorithm->getParameter("adpt::ndb")));
        ui->edADegree->setValue(QString::fromStdString(m_targetAlgorithm->getParameter("adpt::nda")).toDouble());
        ui->edKDelay->setValue(QString::fromStdString(m_targetAlgorithm->getParameter("adpt::kdly")).toDouble());
    }
    catch(iconf_error& e)
    {
        QMessageBox::warning(this, "Error", e.what());
    }
}

void APPAlgorithmDesigner::applySettings()
{
    try
    {
        m_targetAlgorithm->setParameter("adpt::ampoly", ui->edAmPoly->text().toStdString());
        m_targetAlgorithm->setParameter("adpt::ndb", ui->edBPoly->text().toStdString());
        m_targetAlgorithm->setParameter("adpt::nda", ui->edADegree->text().toStdString());
        m_targetAlgorithm->setParameter("adpt::kdly", ui->edKDelay->text().toStdString());
    }
    catch(iconf_error& e)
    {
        QMessageBox::warning(this, "Error", e.what());
    }
}
